package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.WagesMapper;
import com.java.business.model.Wages;
import com.java.business.service.WagesService;
import com.java.common.model.Result;
import com.java.userservice.model.User;
import com.java.userservice.service.api.UserServiceApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WagesServiceImpl implements WagesService {

    @Autowired
    private WagesMapper wagesMapper;

    @DubboReference
    private UserServiceApi userServiceApi;

    @Override
    public Result save(Wages wages) {
        wagesMapper.save(wages);
        return Result.success();
    }

    @Override
    public Result modify(Wages wages) {
        wagesMapper.modify(wages);
        return Result.success();
    }

    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            wagesMapper.remove(id);
        }
        return Result.success();
    }

    @Override
    public Result list(Wages wages) {
        PageHelper.startPage(wages.getPageNum(), wages.getPageSize());

        List<Wages> list = wagesMapper.list(wages);

        for (Wages wag : list) {
            User user = userServiceApi.listAll(Integer.parseInt(wag.getMno()));
            wag.setUser(user);
        }

        PageInfo<Wages> pageInfo = new PageInfo(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());
        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Wages wages = wagesMapper.get(id);
        return Result.success(wages);
    }
}
